<template>
  <div>
    <div class="v-role-manage">
      <div class="module org-list">
        <el-card class="content">
          <div class="title">组织列表</div>
          <div class="search">
            <el-input
              v-model="orgName"
              size="small"
              style="width: 90%"
              placeholder="输入组织名称"
              prefix-icon="el-icon-search"
            />
          </div>
          <div class="list">
            <div
              v-for="org in filterOrgList"
              :key="org.phid"
              :class="{ 'active': activeOrg === org.phid }"
              class="list-item"
              @click="handleClickOrg(org.phid)"
            >
              <el-radio :value="(activeOrg === org.phid)" :label="true">
                {{ org.name }}
              </el-radio>
            </div>
          </div>
        </el-card>
      </div>
      <div class="module auth-list">
        <el-card class="content">
          <div class="title">角色列表</div>
          <div class="search">
            <el-input
              v-model="roleName"
              size="small"
              style="width: 90%"
              placeholder="输入角色名称"
              prefix-icon="el-icon-search"
            />
          </div>
          <div class="list">
            <el-checkbox-group v-model="checkedRole">
              <div
                v-for="role in filterRoleList"
                :key="role.phid"
                :class="{ 'active': activeRole === role.phid }"
                class="list-item"
                @click="handleClickRole(role.phid)"
              >
                <el-checkbox
                  :key="role.phid"
                  :label="role.phid"
                  style="width: 30px"
                ><span>{{ role.name }}</span></el-checkbox>
              </div>
            </el-checkbox-group>
          </div>
        </el-card>
      </div>
      <div class="module auth-info">
        <el-card class="content">
          <div class="title" style="margin-bottom: 10px">权限树</div>
          <auth-tree :role-id="activeRole" :disabled="true" />
        </el-card>
      </div>
    </div>
    <p style="padding: 15px 20px 20px 20px; float: right; eight: 60px">
      <button
        class="greenBtn"
        @click="handleClose"
      >取消</button>
      <button
        class="greenBtn"
        @click="handleSubmit"
      >保存</button>
    </p>
  </div>
</template>

<script>

import { getRoleList, getSelectRuleListById, addUsersRole } from '@/api/role/index'
import { getOrgListByUserId } from '@/api/organization'
import authTree from '@/components/auth-tree/index'

export default {
  name: 'RoleManage',
  components: {
    'auth-tree': authTree
  },
  props: {
    info: {
      type: Array,
      default() {
        return []
      }
    }
  },
  data() {
    return {
      orgList: [], // 组织列表
      orgName: '',
      activeOrg: '',
      roleList: [],
      roleName: '',
      checkedRole: [],
      activeRole: ''
    }
  },
  computed: {
    filterOrgList() {
      return this.orgList.filter(org => org.name.includes(this.orgName))
    },
    filterRoleList() {
      return this.roleList.filter(role => role.name.includes(this.roleName))
    }
  },
  created() {
    this.handleInit()
  },
  methods: {
    handleInit() {
      this.handleGetRoleList().then(() => {
        if (this.roleList.length) {
          this.activeRole = this.roleList[0].phid
        }
      })
      this.handleGetOrgListByUserId(this.info[0].phid).then(() => {
        if (this.orgList.length) {
          this.activeOrg = this.orgList[0].phid
          this.handleGetSelectRuleListById()
        }
      })
    },
    async handleGetOrgListByUserId(id) {
      try {
        const { data } = await getOrgListByUserId(id)
        this.orgList = data || []
      } catch (err) {
        console.error('err', err)
        this.$msgBox.error('获取用户组织失败')
      }
    },
    async handleGetRoleList() {
      try {
        const { data } = await getRoleList()
        this.roleList = data || []
      } catch (err) {
        this.$msgBox.error('获取角色列表失败')
      }
    },
    handleClickOrg(id) {
      this.activeOrg = id
      this.handleGetSelectRuleListById()
    },
    handleClickRole(id) {
      this.activeRole = id
    },
    handleGetSelectRuleListById() {
      const param = {
        usersId: this.info[0].phid,
        orgId: this.activeOrg
      }
      getSelectRuleListById(param).then(({ data }) => {
        this.checkedRole = (data || []).map(role => role.roleId)
      }).catch(err => this.$msgBox.error(err.message))
    },
    handleClose() {
      this.$parent.$emit('update:visible', false)
    },
    handleSubmit() {
      const param = {
        orgId: this.activeOrg,
        roleId: this.checkedRole,
        usersId: this.info[0].phid
      }
      addUsersRole(param).then((res) => {
        this.$msgBox.show(res.message)
        this.handleClose()
      }).catch(() => this.$msgBox.error('用户授权失败'))
    }
  }
}
</script>

<style lang="scss">
@import "@/styles/variables.scss";
.v-role-manage {
  height: 500px;
  display: flex;
  .org-list,
  .auth-list,
  .auth-info {
    flex-basis: 250px;
    margin-right: 20px;
    .content {
      height: 100%;
      overflow: auto;
      .el-card__body {
        padding: 0;
      }
      .list {
        padding: 10px 10px;
        .list-item {
          height: 30px;
          line-height: 30px;
          padding: 0 10px;
          cursor: pointer;
          &:hover {
            background-color:rgba(55, 116, 243, .4);
            border-radius: 5px;
          }
        }
        .list-item.active {
          background-color:rgba(55, 116, 243, .4);
          border-radius: 5px;
        }
      }
    }
    .title {
      height: 40px;
      line-height: 40px;
      text-align: center;
      background-color: #3774F3;
      color: #fff;
    }
    .search {
      text-align: center;
      padding-top: 10px;
    }
  }
  .auth-info {
    flex: 1
  }
}
</style>
<style lang="scss">
</style>
